﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.SysUser
{
    public partial class frmSysUserManager : Form
    {
        public frmSysUserManager()
        {
            InitializeComponent();
        }

        private void SysUserManager_Load(object sender, EventArgs e)
        {
            listViewUser.View = View.Details;
            listViewUser.FullRowSelect = true;
            listViewUser.Columns.Add("编号");
            listViewUser.Columns.Add("用户名", 100);
            listViewUser.Columns.Add("权限", 100);
            UpdateViewUser();
        }

        private void UpdateViewUser()
        {
            listViewUser.Items.Clear();

            List<DbWrapper.DbDataTypes.User> listUser = BusinessWrapper.User.Instance.GetUserList();
            foreach (DbWrapper.DbDataTypes.User user in listUser)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = user.id.ToString();
                lvi.SubItems.Add(user.username);
                int nIndex = 0;
                UserConfigItem item =
                    SYSUSER_DEFINE_DATA.find(SYSUSER_DEFINE_DATA.USER_ROLE, user.roletype, out nIndex);
                if (item != null)
                {
                    lvi.SubItems.Add(item.Text);
                }
                listViewUser.Items.Add(lvi);
            }
        }

        private DbWrapper.DbDataTypes.User GetSelectedUser()
        {
            DbWrapper.DbDataTypes.User user = null;
            if (listViewUser.SelectedItems.Count > 0)
            {
                ListViewItem item = listViewUser.SelectedItems[0];
                int nUserId = Convert.ToInt32(item.Text);
                user = BusinessWrapper.User.Instance.GetUser(nUserId);
            }
            return user;
        }

        private void ToolStripMenuItemDetail_Click(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.User user = GetSelectedUser();
            if (user != null)
            {
                Form dl = new frmSysUserDetail(user);
                dl.ShowDialog();
            }
        }

        private void ToolStripMenuItemModify_Click(object sender, EventArgs e)
        {
             DbWrapper.DbDataTypes.User user = GetSelectedUser();
             if (user != null)
             {
                 Form dl = new frmSysUserModify(user);
                 dl.ShowDialog();
             }
             UpdateViewUser();
        }

        private void ToolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            Form dl = new frmSysUserAdd();
            dl.ShowDialog();
            UpdateViewUser();
        }

        private void contextMenuStripUser_Opening(object sender, CancelEventArgs e)
        {
            bool bSelected = false;
            if (listViewUser.SelectedItems.Count > 0)
            {
                bSelected = true;
            }
            else
            {
                bSelected = false;
            }

            ToolStripMenuItemDetail.Visible = bSelected;
            ToolStripMenuItemModify.Visible = bSelected;
            ToolStripMenuItemDel.Visible = bSelected;
        }

        private void ToolStripMenuItemUserDel_Click(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.User user = GetSelectedUser();
            if (user != null)
            {
                DialogResult result = 
                    MessageBox.Show("是否删除该用户", 
                        "告警", 
                        MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    bool bSuccess = BusinessWrapper.User.Instance.DelUser(user);
                    if (bSuccess)
                    {
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                    UpdateViewUser();
                }
            }
        }

        private void listViewUser_DoubleClick(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.User user = GetSelectedUser();
            if (user != null)
            {
                Form dl = new frmSysUserDetail(user);
                dl.ShowDialog();
            }
        }
    }
}
